import Avatar from '@/static/images/avatar.png'
import { defineStore } from 'pinia'
import { ref } from 'vue'

const initState = { username: '木糖醇', avatar: Avatar, phone: '1997****962' }

function storeSetup() {
  const userInfo = ref<IUserInfo>({ ...initState })

  const setUserInfo = (val: IUserInfo) => {
    userInfo.value = val
  }

  const clearUserInfo = () => {
    userInfo.value = { ...initState }
  }

  // 一般没有reset需求，不需要的可以删除
  const reset = () => {
    userInfo.value = { ...initState }
  }

  const isLogined = computed(() => !!userInfo.value.token)

  return {
    userInfo,
    setUserInfo,
    clearUserInfo,
    isLogined,
    reset,
  }
}

export const useUserStore = defineStore('user', storeSetup, { persist: true })
